package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.spa.infrastructure.repository.po.CitiesServedPO;
import com.spa.infrastructure.util.StringUtils;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.repository.condition.MassagistCondition;
import com.spa.infrastructure.repository.mapper.MassagistMapper;
import com.spa.infrastructure.repository.po.MassagistPO;
import com.spa.infrastructure.repository.po.ShopPO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
@Data
public class MassagistRepositoryService extends MPJBaseServiceImpl<MassagistMapper, MassagistPO> {

    @Resource
    private MassagistMapper massagistMapper;

    public Page<MassagistPO> page(MassagistCondition condition) {
        LambdaQueryWrapper<MassagistPO> wrapper = wrapper(condition);
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);
    }

    public List<MassagistPO> list(MassagistCondition condition) {
        return list(wrapper(condition));
    }

    public Integer addMassagist(MassagistPO massagistPO) {
        return massagistMapper.insert(massagistPO);
    }

    public int updateMassagistById(MassagistPO massagistPO) {
      return  massagistMapper.updateById(massagistPO);
    }

    public MassagistPO getOneById(Long id) {
        return massagistMapper.selectById(id);
    }

    public MassagistPO getOneByCondition(MassagistCondition condition) {
        List<MassagistPO> list = list(condition);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    private LambdaQueryWrapper<MassagistPO> wrapper(MassagistCondition condition) {
        LambdaQueryWrapper<MassagistPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(condition.getPhone()), MassagistPO::getPhone, condition.getPhone());
        wrapper.eq(ObjectUtil.isNotNull(condition.getStatus()), MassagistPO::getStatus, condition.getStatus());
        wrapper.eq(ObjectUtil.isNotNull(condition.getMassagistId()), MassagistPO::getId, condition.getMassagistId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getUserId()), MassagistPO::getUserId, condition.getUserId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getShopId()), MassagistPO::getShopId, condition.getShopId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getTag()), MassagistPO::getTag, condition.getTag());
        wrapper.in(CollUtil.isNotEmpty(condition.getMassagistIds()), MassagistPO::getId, condition.getMassagistIds());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getType()),MassagistPO::getType, condition.getType());
        wrapper.eq(ObjectUtils.isNotEmpty(condition.getId()),MassagistPO::getId, condition.getId());
        wrapper.like(StringUtils.isNotEmpty(condition.getName()),MassagistPO::getName, condition.getName());
        wrapper.in(CollUtil.isNotEmpty(condition.getInStatus()), MassagistPO::getStatus, condition.getInStatus());
        wrapper.eq(MassagistPO::getIsDel, ObjectUtils.isNotEmpty(condition.getIsDel())?condition.getIsDel():WhetherTypeEnum.$0.getCode());
        wrapper.in(CollUtil.isNotEmpty(condition.getShopIds()),MassagistPO::getShopId,condition.getShopIds());
        wrapper.orderByDesc(MassagistPO::getStatus);
        return wrapper;
    }

    private MPJLambdaWrapper<MassagistPO> joinShopWrapper(MassagistCondition condition) {
        MPJLambdaWrapper<MassagistPO> wrapper = JoinWrappers.lambda(MassagistPO.class)
        .selectAll(MassagistPO.class)//查询表全部字段
        .selectAs(ShopPO::getName,"shopName")
                .selectAs(CitiesServedPO::getCityName, "cityName")
        .leftJoin(ShopPO.class, ShopPO::getId, MassagistPO::getShopId)
                .leftJoin(CitiesServedPO.class, CitiesServedPO::getId, ShopPO::getCityId);
        wrapper.like(ObjectUtil.isNotEmpty(condition.getName()),MassagistPO::getName, condition.getName());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getPhone()), MassagistPO::getPhone, condition.getPhone());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getShopId()),ShopPO::getId, condition.getShopId());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getCityId()),ShopPO::getCityId, condition.getCityId());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getStatus()),MassagistPO::getStatus, condition.getStatus());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getTag()),MassagistPO::getTag, condition.getTag());
        wrapper.eq(MassagistPO::getIsDel, ObjectUtils.isNotEmpty(condition.getIsDel())?condition.getIsDel():WhetherTypeEnum.$0.getCode());
        wrapper.orderByDesc(condition.isSortCreateTimeDesc(),MassagistPO::getCreateTime);
        return wrapper;
    }

    public boolean massagistExistsByStatus(MassagistCondition condition) {
        return massagistMapper.exists(wrapper(condition));
    }

    public boolean updateMassagitByCondition(MassagistPO po) {
        return update(null, updateWrapper(po));
    }

    public Page<MassagistPO> pageJoinShopByCondition(MassagistCondition condition) {
        MPJLambdaWrapper<MassagistPO> wrapper = joinShopWrapper(condition);
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);
    }

    public Page<MassagistPO> pageJoinShopMassagistRef(MassagistCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), joinShopMassagistRefWrapper(condition));
    }

    private MPJLambdaWrapper<MassagistPO> joinShopMassagistRefWrapper(MassagistCondition condition) {
        MPJLambdaWrapper<MassagistPO> wrapper = JoinWrappers.lambda(MassagistPO.class)
                .selectAll(MassagistPO.class)//查询表全部字段
                .leftJoin(ShopPO.class, ShopPO::getId, MassagistPO::getShopId);
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getStatus()), MassagistPO::getStatus, condition.getStatus());
        wrapper.eq(MassagistPO::getIsDel, ObjectUtils.isNotEmpty(condition.getIsDel())?condition.getIsDel():WhetherTypeEnum.$0.getCode());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getShopId()), MassagistPO::getShopId, condition.getShopId());
        wrapper.eq(ObjectUtil.isNotEmpty(condition.getTag()), MassagistPO::getTag, condition.getTag());
        wrapper.in(CollUtil.isNotEmpty(condition.getInStatus()), MassagistPO::getStatus, condition.getInStatus());
        wrapper.orderByDesc(MassagistPO::getCreateTime);
        return wrapper;
    }

    public List<MassagistPO> massagistPOAllList(MassagistCondition condition){
        return list(joinShopWrapper(condition));
    }

    private LambdaUpdateWrapper<MassagistPO> updateWrapper(MassagistPO po) {
        LambdaUpdateWrapper<MassagistPO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ObjectUtil.isNotNull(po.getId()), MassagistPO::getId, po.getId());
        updateWrapper.set(ObjectUtil.isNotNull(po.getIsDel()), MassagistPO::getIsDel, po.getIsDel());
        updateWrapper.set(ObjectUtil.isNotNull(po.getStatus()), MassagistPO::getStatus, po.getStatus());
        updateWrapper.set(ObjectUtil.isNotNull(po.getAppointmentTime()), MassagistPO::getAppointmentTime, po.getAppointmentTime());
        updateWrapper.set(ObjectUtil.isNotEmpty(po.getLat()), MassagistPO::getLat, po.getLat());
        updateWrapper.set(ObjectUtil.isNotEmpty(po.getLon()), MassagistPO::getLon, po.getLon());
        updateWrapper.set(ObjectUtil.isNotEmpty(po.getName()), MassagistPO::getName, po.getName());
        updateWrapper.set(ObjectUtil.isNotEmpty(po.getIdCardTails()), MassagistPO::getIdCardTails, po.getIdCardTails());
        updateWrapper.set(ObjectUtil.isNotEmpty(po.getIdCardFront()), MassagistPO::getIdCardFront, po.getIdCardFront());
        updateWrapper.set(ObjectUtil.isNotEmpty(po.getWorkPhoto()), MassagistPO::getWorkPhoto, po.getWorkPhoto());
        updateWrapper.set(ObjectUtil.isNotEmpty(po.getLifePhoto()), MassagistPO::getLifePhoto, po.getLifePhoto());
        updateWrapper.set(ObjectUtil.isNotEmpty(po.getQualificationCertificate()), MassagistPO::getQualificationCertificate, po.getQualificationCertificate());
        updateWrapper.set(StringUtils.isNotEmpty(po.getHealthCertificate()), MassagistPO::getHealthCertificate, po.getHealthCertificate());
        updateWrapper.set(StringUtils.isNotEmpty(po.getScore()), MassagistPO::getScore, po.getScore());
        return updateWrapper;
    }
}
